<?php
namespace app\index\controller;
use app\common\controller\Base;
use app\common\model\Datee as DateeModel;
use think\Session;

class Datee extends Base
{
	public function dateList(){
		$list = DateeModel::all();
		$this->view->assign('list', $list);
		$this -> view -> count = sizeof($list);
		return $this -> view -> fetch('date_list');
    }

    public function dateAdd(){

    	$data = $this->request->param();

    	$this->view->assign('title','添加日期');
    	$this->view->assign("data",$data);
    	return $this -> view -> fetch('date_add');
    }

    public function addDatee(){

    	$data = $this->request->param();
		$data_p = $this->request->post();

		

		$k = 0;
        $ids = 0;
		// return $data;
		foreach ($data_p as $key => $value) {
            # code...
            $pos = strpos($key,'_');
            $typee = substr($key,0,$pos);  //得到字段类型，批发还是零售
            $ww = substr($key,$pos+1);  //得到week

            $arr[$k]['yy'] = $data['yy'];
            $arr[$k]['mm'] = $data['mm'];
            $arr[$k]['ww'] = $ww;
            $arr[$k][$typee] = ($value=="") ? 0 : $value;
            
            $ids += 1;
            if ($ids == 4) {
                $k+=1;
                $ids =0;
            }
        }
        // return $arr;

        foreach ($arr as $key => $value) {
        	$check = DateeModel::where(['yy'=>$value['yy'],'mm'=>$value['mm'],'ww'=>$value['ww']])->find();
            if (!empty($check)) {
            	DateeModel::where([
            		'yy'=>$value['yy'],
            		'mm'=>$value['mm'],
            		'ww'=>$value['ww'] ])
            	->update([
            		'day'=>$value['day'],
	                'dayacc'=>$value['dayacc'],
	                'wkday'=>$value['wkday'],
	                'wkdayacc'=>$value['wkdayacc'] ]);
            }else{
            	DateeModel::create([
                'yy'=>$value['yy'],
                'mm'=>$value['mm'],
                'ww'=>$value['ww'],
                'day'=>$value['day'],
                'dayacc'=>$value['dayacc'],
                'wkday'=>$value['wkday'],
                'wkdayacc'=>$value['wkdayacc']
            							]);
            }
        };
    	return ['status'=>1, 'message'=>'日期数据更新成功'];
    }

    public function dateDel(){
    	$data = $this->request->param();
    	
    	DateeModel::where(['yy' => $data['yy'],'mm' => $data['mm']])->delete();

    }
    
}